//
//  ;
//  programme-robot-1
//
//  Created by richard on 11/09/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "programme_robot_1AppDelegate.h"
#define BTB @"0016530DF404"
@implementation programme_robot_1AppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	// Insert code here to initialize your application 
	
	/*R1=[[NXTcommande alloc] init];
	[R1 connect:BTB];
	R2=[[NXTcommande alloc] init];
	[R2 connect:BTB];
	NSLog(@"%@",R1);
	NSLog(@"%@",R2);*/

	/*char *message = malloc(3*sizeof(char));
	*message = 0x08;
	char *ptr = message +1;
	*ptr = 0x08;
	ptr = message +2;
	*ptr = 0x08;
	
	//sleep(2);
	
	
	
	
	
	NSLog(@"%@",test1);
	for (int i=0 ; i< 100  ; i++) {
		//	sleep(2);
		
		*ptr=i;
		[test1 sendMessage:message length:3];
		NSLog (@"--> %d",*(message+2));
	}*/
	
	//test wiimote 
	/*W1=[[wiimoteCommande alloc] init];
	[W1 connect];*/
	/*W2=[[wiimoteCommande alloc] init];
	[W2 connect];*/
	
	NSTextField *T1 = (NSTextField *)  connectionB1OK;
	//[T1.cell setString:@"#"];
	[T1.cell setTextColor:[NSColor redColor]];
	T1 = (NSTextField *)  connectionB2OK;
	//[T1.cell setString:@"#"];
	[T1.cell setTextColor:[NSColor redColor]];
	
	wiiId = 0;
}
/**
 Wiimotecommmande delegate
 - (void) afficheAdresse:(NSString *) adresse;

 **/
- (void) sendMoveStop:(int ) robot{
	NSLog(@" Move robot %d %@ %@",robot,R1,R2);
	/*NXTcommande * R0 = R1;
	if (robot ==2 ) {
		R0 = R2;
	}*/
	char *info = malloc(3*sizeof(char));
	*info = 0x0;
	char *ptr = info +1;
	*ptr = 0x0;
	ptr = info +2;
	*ptr = 0x0;
	[R1 sendMessage:info length:3];
	[R2 sendMessage:info length:3];

}

- (void) sendMoveLeft:(int ) robot{
	NSLog(@" Move robot %d",robot);
	/*NXTcommande * R0 = R1;
	if (robot ==2 ) {
		R0 = R2;
	}*/
	char *info = malloc(3*sizeof(char));
	*info = 0x02;
	char *ptr = info +1;
	*ptr = 0x02;
	ptr = info +2;
	*ptr = 0x02;
	[R1 sendMessage:info length:3];
	[R2 sendMessage:info length:3];

}
- (void) sendMoveRight:(int ) robot{
	/*NXTcommande * R0 = R1;
	if (robot ==2 ) {
		R0 = R2;
	}*/
	char *info = malloc(3*sizeof(char));
	*info = 0x04;
	char *ptr = info +1;
	*ptr = 0x04;
	ptr = info +2;
	*ptr = 0x04;
	[R1 sendMessage:info length:3];
	[R2 sendMessage:info length:3];

}
- (void) sendMoveRacket:(int ) robot{
/*	NXTcommande * R0 = R1;
	if (robot ==2 ) {
		R0 = R2;
	}*/
	char *info = malloc(3*sizeof(char));
	*info = 0x08;
	char *ptr = info +1;
	*ptr = 0x08;
	ptr = info +2;
	*ptr = 0x08;
	[R1 sendMessage:info length:3];
	[R2 sendMessage:info length:3];

}

- (void) afficheAdresse:(NSString *) adresse{
	NSLog(@"%@",adresse);
	
	NSTextField *T1 = (NSTextField *)  connectionB1OK;
	if (wiiId == 2){
		T1 = (NSTextField *)  connectionB2OK;
	}
	//[T1.cell setString:@"#"];
	[T1.cell setTextColor:[NSColor greenColor]];
	T1 = (NSTextField *) adresseB1;
	if (wiiId == 2){
		T1 = (NSTextField *)  adresseB2;
	}
	[T1 setStringValue:adresse];
	
	wiiId = 0;
}
- (IBAction)ConnectW1:(id)sender{
	W1=[[wiimoteCommande alloc] init];
	//[W1 connect:@"0017ab3922e6"];
	W1.delegate=self;
	wiiId = 1;
	[W1 connect];
	W1.robot = 1;
	
}
- (IBAction)ConnectW2:(id)sender{
	W2=[[wiimoteCommande alloc] init];
	
	W2.delegate=self;
	wiiId = 2;
	[W2 connect];
	W2.robot = 2;
}
- (void) connectFromWii:(int ) robot{
	NSLog(@" COnnection %d ",robot);
	/*if (robot ==1 ){
				[self  ConnectR1:self];
	}
	if (robot ==2 ) {
		
		[self  ConnectR2:self];
	}*/
	if (R1 == nil) [self  ConnectR1:self];
	else [self  ConnectR2:self];
	
	
}
- (IBAction)ConnectR1:(id)sender{
	R1=[[NXTcommande alloc] init];
	R1.delegate = self;
	[R1 connect:BTB];
	NSLog(@"--NXT 1 %@",R1);

}
- (IBAction)ConnectR2:(id)sender{
	R2=[[NXTcommande alloc] init];
	R2.delegate = self;
	[R2 connect:BTB];
	NSLog(@"--NXT %@",R1);
	
}

- (IBAction)SendR1:(id)sender{
	NSTextField * cmdeT = ((NSTextField  *) message);
	int cmde = [[cmdeT  stringValue] intValue];
	char *info = malloc(3*sizeof(char));
	*info = cmde;
	char *ptr = info +1;
	*ptr = cmde;
	ptr = info +2;
	*ptr = cmde;
	[R1 sendMessage:info length:3];
	
}
- (IBAction)SendR2:(id)sender{
	NSTextField * cmdeT = ((NSTextField  *) message);
	int cmde = [[cmdeT  stringValue] intValue];
	char *info = malloc(3*sizeof(char));
	*info = cmde;
	char *ptr = info +1;
	*ptr = cmde;
	ptr = info +2;
	*ptr = cmde;
	[R2 sendMessage:info length:3];
}

- (IBAction)ShowWiimote:(id)sender{
	[((NSPanel * ) wiimoteWindow) makeKeyAndOrderFront:nil]; 
}
- (IBAction)ShowNXT:(id)sender{
	[((NSPanel * ) nxtWindow) makeKeyAndOrderFront:nil]; 
}

// Protocole NXT
- (void)afficheinfoNXT:(NSString *)info{
}
@end
